<template>
  <div>
  <el-card shadow="always" class="el-card-login">
    <img src='../assets/logo.png'>
    <h1>人流量统计</h1>
    <!-- label-position 用于改变表单域的标签位置 可以为 left right top -->
    <el-form :model="ruleForm" :label-position="labelPosition" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="用户名" prop="username" class="input1">
        <el-input type="textword" v-model="ruleForm.username" autocomplete="off">
        </el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password" class="input2">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off">
        </el-input>
    </el-form-item>
    <el-form-item>
        <el-button type="primary" @click="login()">登录</el-button>
        <!-- primary 鼠标滑过/点击背景会变深色调  -->
        <el-button @click="register()">注册</el-button>
    </el-form-item>
    </el-form>
  </el-card>
  </div>
</template>

<script>
export default {
  data () {
    var username = (value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'))
      } else {
        if (this.ruleForm.username !== '') {
          this.$refs.ruleForm.validateField('username')
          // validataField 会校验表单的部分字段
        }
        callback()
      }
    }
    var password = (value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    return {
      labelPosition: 'right', // 右对齐
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { validator: username, trigger: 'blur' },
          { min: 3, max: 10, message: '请输入长度 3-10 的字符串' }
        ],
        password: [
          { validator: password, trigger: 'blur' },
          { min: 8, max: 20, message: '请输入长度 8-20 的字符串' }
        ]
      }
    }
  },
  methods: {
    login () {
      if (this.ruleForm.username === '' || this.ruleForm.password === '') {
        window.alert('账号或密码不能为空！')
        return
      }
      var username = this.ruleForm.username
      var password = this.ruleForm.password
      this.$http.post('/api/user/login', {
        username: username,
        password: password
      }, {}).then((response) => {
        // console.log(response) // 不显示防止暴露密码
        if (response.body.length === 0) {
          window.alert('账号或密码错误！')
        } else {
          // window.alert('登录成功！')
          sessionStorage.setItem('username', this.username)
          this.$emit('login', 1)
          this.$router.push('/home')
        }
      })
    },
    // 注册
    register () {
      this.$router.push('/register') // 页面跳转
    }
  }
}
</script>

<style>
.el-card-login {
  width: 500px;
  height: 500px;
  margin: 0px auto;
  border-radius: 30px;
}
.el-card-login h1{
  margin-bottom: 30px;
}
/* 超小设备 (手机, 600px 以下屏幕设备) */
@media only screen and (max-width: 600px) {
    .el-card-login {
    /* background: red; */
    width: 100%;
    margin: 0px auto;
    }
}
/* 小设备 (平板电脑和大型手机，542 像素及以上) */
@media only screen and (min-width: 542px) {
    .el-card-login {
    /* background: green; */
    width: 70%;
    margin: auto;
    }
}
/* 大设备 956px 及其以上 */
@media only screen and (min-width: 956px) {
    .el-card-login {
    /* background: green; */
    width: 50%;
    margin: 10px auto;
    }
}
/* 超大型设备（大型笔记本电脑和台式机，1200 像素及以上） */
@media only screen and (min-width: 1200px) {
    .el-card-login {
    width: 50%;
    margin: 20px auto;
    }
}
</style>
